package com.rgp.servicedriveruser.service.impl;

import com.rgp.internalcommon.common.CommonStatusEnum;
import com.rgp.internalcommon.common.DriverCarBindingRelationshipConstant;
import com.rgp.internalcommon.dto.ResponseResult;
import com.rgp.internalcommon.response.DriverCarResponse;
import com.rgp.servicedriveruser.mapper.CarMapper;
import com.rgp.servicedriveruser.mapper.DriverCarBindingRelationshipMapper;
import com.rgp.servicedriveruser.pojo.Car;
import com.rgp.servicedriveruser.pojo.DriverCarBindingRelationship;
import com.rgp.servicedriveruser.service.DriverCarBindingRelationshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author nhp
 * @version 1.0
 * @description TODO
 * @date 2024-04-16 21:05
 */
@Service
public class DriverCarBindingRelationshipServiceImpl implements DriverCarBindingRelationshipService {

    @Autowired
    private DriverCarBindingRelationshipMapper bindingRelationshipMapper;

    @Autowired
    private CarMapper carMapper;

    @Override
    public ResponseResult bind(DriverCarBindingRelationship driverCarBindingRelationship) {
        //1.绑定之前先查看是否存在该绑定关系
        Integer existBind = bindingRelationshipMapper.checkBindState(driverCarBindingRelationship);
        if (existBind > 0){
            return ResponseResult.fail(CommonStatusEnum.EXIST_DRIVER_CAR_RELATIONSHIP_ERROR);
        }
        //2.bind
        driverCarBindingRelationship.setBindState(DriverCarBindingRelationshipConstant.BIND);
        driverCarBindingRelationship.setBindingTime(LocalDateTime.now());
        int result = bindingRelationshipMapper.insert(driverCarBindingRelationship);
        if (result == 1){
            return ResponseResult.success();
        }else{
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_RELATIONSHIP_FAIL_BIND_ERROR);
        }
    }

    @Override
    public ResponseResult unbind(DriverCarBindingRelationship driverCarBindingRelationship) {

        Map<String, Object> map = new HashMap<>();
        map.put("driver_id",driverCarBindingRelationship.getDriverId());
        map.put("car_id",driverCarBindingRelationship.getCarId());
        map.put("bind_state",DriverCarBindingRelationshipConstant.BIND);
        List<DriverCarBindingRelationship> list = bindingRelationshipMapper.selectByMap(map);
        if (list == null || list.size() == 0){
            return ResponseResult.fail(CommonStatusEnum.NOT_EXIST_DRIVER_CAR_RELATIONSHIP_ERROR);
        }else if(list.get(0).getBindState().equals(DriverCarBindingRelationshipConstant.UNBIND)){
            //绑定关系已解除
            return ResponseResult.success();
        }
        DriverCarBindingRelationship bind = list.get(0);
        bind.setBindState(DriverCarBindingRelationshipConstant.UNBIND);
        bind.setUnBindingTime(LocalDateTime.now());

        int result = bindingRelationshipMapper.updateById(bind);
        if (result == 1){
            return ResponseResult.success();
        }
        return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_RELATIONSHIP_FAIL_UNBIND_ERROR);
    }

    @Override
    public ResponseResult<List<DriverCarResponse>> getDriversByCarIds(List<String> carIds) {
        //1.根据车辆id查询车牌号
        List<Car> carList = carMapper.getCarInfoByCarIds(carIds);

        //2.根据车辆id查询司机id
        List<DriverCarResponse> response = bindingRelationshipMapper.getAvailableDriversByCarIds(carIds);

        //key：carId，value：vehicleNo
        //key：carId，value：vehicleType
        HashMap<Long, String> vehicleNoMap = new HashMap<>();
        HashMap<Long, String> vehicleTypeMap = new HashMap<>();
        for (Car car : carList){
            vehicleNoMap.put(car.getId(),car.getVehicleNo());
            vehicleTypeMap.put(car.getId(),car.getVehicleType());
        }
        response.stream().forEach(item-> {
            item.setVehicleNo(vehicleNoMap.get(item.getCarId()));
            item.setVehicleType(vehicleTypeMap.get(item.getCarId()));
        });
        //返回司机id、车辆id、车牌号
        return ResponseResult.success(response);
    }
}